package cn.akira.domain.strategy.service.rule.chain.impl;

import cn.akira.domain.strategy.repository.IStrategyRepository;
import cn.akira.domain.strategy.service.rule.chain.AbstractLogicChain;
import cn.akira.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import cn.akira.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author fjl95
 * @date 2025/5/15
 * @description 黑名单规则校验链
 */
@Slf4j
@Component("rule_blacklist")
public class BlackListLogicChain extends AbstractLogicChain {


    @Resource
    private IStrategyRepository strategyRepository;
    @Override
    protected String ruleModel() {
        return "rule_blacklist";
    }

    @Override
    public DefaultChainFactory.StrategyAwardVO logic(String userId, Long strategyId) {
        String ruleValue = strategyRepository.queryStrategyRuleValue(strategyId,ruleModel());
        String[] splirRuleValue = ruleValue.split(Constants.COLON);
        //拿到默认奖品
        Integer awardId = Integer.parseInt(splirRuleValue[0]);
        //拿到黑名单列表
        String[] userBlackIds = splirRuleValue[1].split(Constants.SPLIT);
        for (String userBlackId : userBlackIds) {
            //若当前用户在黑名单
            if (userId.equals(userBlackId)) {
                log.info("抽奖责任链-黑名单接管 userId: {} strategyId: {} ruleModel: {} awardId: {}", userId, strategyId, ruleModel(), awardId);
                return DefaultChainFactory.StrategyAwardVO.builder()
                        .awardId(awardId)
                        .logicModel(ruleModel())
                        .build();
            }
        }

        // 过滤其他责任链
        log.info("抽奖责任链-黑名单放行 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());
        //传给下一个链
        return next().logic(userId, strategyId);

    }
}
